﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http

Public Class PhlogiosMain

    Private Channel As TcpServerChannel
    'Private HttpChannel As HttpServerChannel

    Sub HandleUnhandled(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        System.Diagnostics.EventLog.WriteEntry("Phlogios", e.ExceptionObject.ToString, EventLogEntryType.Error)
    End Sub

    Friend Sub DebugStart(ByVal args() As String)
        OnStart(args)
    End Sub

    Friend Sub DebugEnd()
        OnStop()
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        MyBase.OnStart(args)

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf HandleUnhandled

        If Channel Is Nothing Then
            Dim kitchenSink As New BinaryServerFormatterSinkProvider
            kitchenSink.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full

            RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off
            RemotingConfiguration.ApplicationName = My.Settings.RemotingName

            Channel = New TcpServerChannel(My.Settings.RemotingName, My.Settings.RemotingPort, kitchenSink)

            ChannelServices.RegisterChannel(Channel, False)

            Dim executorEntry As New WellKnownServiceTypeEntry(GetType(ActionHub), My.Settings.RemotingURI, WellKnownObjectMode.Singleton)
            RemotingConfiguration.RegisterWellKnownServiceType(executorEntry)

        Else
            Channel.StartListening(Nothing)
        End If
    End Sub

    Protected Overrides Sub OnStop()
        MyBase.OnStop()
        Channel.StopListening(Nothing)
    End Sub


End Class
